import os
import sys
import mutagen


if len(sys.argv) != 2:
    print("Usage: python3 getmusic.py CD_path")
    exit()

os.chdir(os.path.join("/home/lrf/shark/music", sys.argv[1]))
root = os.path.join("music", sys.argv[1])

track_list = dict()
for path in os.listdir():
    if os.path.isfile(path) and path.endswith(".flac"):
        track_list[path] = mutagen.File(path)

if "cover.jpg" in os.listdir():
    cover = os.path.join(root, "cover.jpg")
elif "Cover.jpg" in os.listdir():
    cover = os.path.join(root, "cover.jpg")
elif "cover.jpeg" in os.listdir():
    cover = os.path.join(root, "cover.jpg")
elif "Cover.jpeg" in os.listdir():
    cover = os.path.join(root, "cover.jpg")
elif "cover.png" in os.listdir():
    cover = os.path.join(root, "cover.jpg")
elif "Cover.png" in os.listdir():
    cover = os.path.join(root, "cover.jpg")

for i in range(1, len(track_list) + 1):
    for path in track_list.keys():
        if int(track_list[path]["tracknumber"][0]) == i:
            name = track_list[path]["title"][0]
            if "artist" in track_list[path].keys():
                artist = track_list[path]["artist"][0]
            else:
                artist = ""
            url = os.path.join(root, path)
            print('{{name: "{0}",artist: "{1}",url: "{2}",cover: "{3}"}},'.format(name, artist, url, cover))